//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;
using OpenLS.Spreadsheet.Functions;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.UI;

namespace OpenLS.Spreadsheet.UI
{
    static class GridFormulaEditing
    {
        public static void EnableCommands(UIElement grid)
        {
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.InsertFunction,
                OnInsertFunction, WorksheetHelper.OnHasActiveCell));
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.AutoSum,
                OnAutoSum, WorksheetHelper.OnHasSelection));

        }
        static void OnAutoSum(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet worksheet = GetWorksheet(sender);
            var functionName = args.Parameter as String;
            if (functionName == null)
                functionName = "SUM";
            worksheet.Selection.AutoSum(functionName);
        }
        internal static void OnInsertFunction(object sender, ExecutedRoutedEventArgs args)
        {
            InsertFunction(sender, args.Parameter as Function);
            args.Handled = true;
        }
        internal static void InsertFunction(object sender, Function f)
        {
            Worksheet Worksheet = SpreadsheetElement.GetWorksheet(sender);
            if (Worksheet == null)
            {
                Workbook workbook = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
                Worksheet = workbook.View.ActiveSheet as Worksheet;
                if (Worksheet == null)
                    return;
            }
            if (f == null)
            {
                InsertFunctionControl d = new InsertFunctionControl();
                WindowServices.ShowDialog(d, SpreadsheetCommands.InsertFunction.Text,
                    sender as UIElement,
                    delegate
                {
                    f = d.SelectedFunction;
                    InsertFunctionImp(sender, Worksheet, f);
                }, o => false)
                ;
                //else
                    return;
            }
            InsertFunctionImp(sender, Worksheet, f);
        }

        private static void InsertFunctionImp(object sender, Worksheet worksheet, Function f)
        {
            WorksheetViewModel worksheetViewModel = SpreadsheetElement.GetWorksheetViewModel((DependencyObject)sender);
            string functionName = f.LocalizedName;
            if (worksheet.ActiveCell.Formula == null)
            {
                EditionElement.SetEditedCellFor(sender, new EditedCell(worksheetViewModel, worksheet.ActiveCell, "=" + functionName + "()"));

            }
            else
                worksheet.ActiveCell.Formula += functionName;
        }

        private static Worksheet GetWorksheet(object sender)
        {
            return WorksheetHelper.GetWorksheet(sender);
        }
    }
}
